package tool.library.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ThemedSpinnerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xzp on 15/9/18.
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final int LOAD_VIEW_TYPE=10086;
    protected List<T> mList;
    private Context mContext;
    private OnItemClickListener mClickListener;
    private RecyclerView.ViewHolder mLoadHolder;
    public BaseRecyclerAdapter(Context context)
    {
        this(context,null);
    }
    public BaseRecyclerAdapter(Context context,List<T> list)
    {
        this.mContext=context;
        this.mList=list;
    }
    public void setList(List<T> list)
    {
        mList=list;
        this.notifyDataSetChanged();
    }
    public void addAll(List<T> list)
    {
        if(mList==null)
            mList=list;
        else{
            if(list!=null)
                mList.addAll(list);
        }
        this.notifyDataSetChanged();
    }
    public void add(T object)
    {
        if(mList==null)
            mList=new ArrayList<T>();
        mList.add(object);
        this.notifyDataSetChanged();
    }
    public void clear()
    {
        if(mList!=null)
        {
            mList.clear();
            this.notifyDataSetChanged();
        }
    }
    public List<T> getList() {
        return mList;
    }
    public List<T> getListOrCreate(){
        if(mList==null)
            mList=new ArrayList<T>();
        return mList;
    }
    public int getListSize(){
        if(mList==null)
            return 0;
        else
            return mList.size();
    }
    public Context getContext() {
        return mContext;
    }
    @Override
    public int getItemCount() {
        int count = mList==null?0:mList.size();
        if(mLoadHolder!=null)
            count++;
        return count;
    }

    @Override
    public final int getItemViewType(int position) {
        if(mLoadHolder!=null && position==getItemCount()-1)
            return LOAD_VIEW_TYPE;
        else
            return getViewType(position);

    }
    public int getViewType(int position){
        return 0;
    }



    public void setOnItemClickListener(OnItemClickListener onItemClickListener)
    {
        this.mClickListener=onItemClickListener;
    }

    public void setLoadHolder(RecyclerView.ViewHolder loadHolder) {
        this.mLoadHolder = loadHolder;
    }

    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==LOAD_VIEW_TYPE)
        {
            return mLoadHolder;
        }
        return onCreateHolder(parent, viewType);
    }

    public abstract RecyclerView.ViewHolder onCreateHolder(ViewGroup parent,int viewType);

    @Override
    public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==LOAD_VIEW_TYPE)
            return;
        onBindHolder(holder,position);
    }
    public abstract void onBindHolder(RecyclerView.ViewHolder holder, int position);
    public class ClickableViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public ClickableViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(this.getAdapterPosition()!=RecyclerView.NO_POSITION && mClickListener!=null)
                mClickListener.onItemClick(BaseRecyclerAdapter.this,v,this.getAdapterPosition());

        }
    }

    public interface OnItemClickListener{
        void onItemClick(BaseRecyclerAdapter adapter,View item,int position);
    }
}
